package com.maple.utils.util

import android.text.TextUtils
import com.blankj.utilcode.util.GsonUtils
import com.maple.utils.const.ConstKey
import com.tencent.mmkv.MMKV

class StoreHelper private constructor() {


    companion object {

        private val kv: MMKV? by lazy { MMKV.defaultMMKV() }
        private val gson by lazy { GsonUtils.getGson() }

        fun saveUserInfo(isSaved: Boolean) :Boolean{
            return kv?.encode(ConstKey.SaveInfoKey.USER_INFO,if(isSaved) "YES" else "") ?: false
        }

        fun hasUserInfo(): Boolean{
            val value = kv?.getString(ConstKey.SaveInfoKey.USER_INFO, "")
            if(TextUtils.equals(value,"YES")) {
                return true
            }
            return false
        }

        fun saveAppFirst() :Boolean{
            return kv?.encode(ConstKey.SaveInfoKey.HAS_APP_FIRST,"YES") ?: false
        }

        fun isStartup(): Boolean{
            val value = kv?.getString(ConstKey.SaveInfoKey.HAS_APP_FIRST, "")
            if(TextUtils.equals(value,"YES")) {
                return true
            }
            return false
        }

        fun saveAppUsed(isUsed: Boolean) :Boolean{
            return kv?.encode(ConstKey.SaveInfoKey.HAS_APP_USED,if(isUsed) "YES" else "") ?: false
        }

        fun hasAppUsed(): Boolean{
            val value = kv?.getString(ConstKey.SaveInfoKey.HAS_APP_USED, "")
            if(TextUtils.equals(value,"YES")) {
                return true
            }
            return false
        }

        fun saveAppAgree(isSaved: Boolean) :Boolean{
            return kv?.encode(ConstKey.SaveInfoKey.HAS_APP_AGREE,if(isSaved) "YES" else "") ?: false
        }

        fun hasAppAgree(): Boolean{
            val value = kv?.getString(ConstKey.SaveInfoKey.HAS_APP_AGREE, "")
            if(TextUtils.equals(value,"YES")) {
                return true
            }
            return false
        }

//        fun saveUserInfo(user: UserInfoEntity.Data?): Boolean {
//            if (user == null) return false
//            try {
//                return kv?.encode(KEY_USER_INFO, gson?.toJson(user)) ?: false
//            } catch (e: Exception) {
//                e.fillInStackTrace()
//                LogUtils.logGGQ("--保存用户异常-->${e.fillInStackTrace()}")
//            }
//            return false
//        }
//
//        fun getUserInfo() : UserInfoEntity.Data?{
//            val json: String? = kv?.getString(KEY_USER_INFO, "")
//            if (!TextUtils.isEmpty(json)) {
//                try {
//                    return gson.fromJson(json,UserInfoEntity.Data::class.java)
//                } catch (e: Exception) {
//                    e.fillInStackTrace()
//                    LogUtils.logGGQ("--获取用户异常-->${e.fillInStackTrace()}")
//                }
//            }
//            return null
//        }
    }
}